//
//  HMHomeViewModel.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/3.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMHomeViewModel.h"

#import "HMNetWorkTool.h"
#import <YYModel.h>
@interface HMHomeViewModel ()

@property(nonatomic,strong)NSArray* sectionChannelList;

@end

@implementation HMHomeViewModel


-(NSDictionary*)channelSectionInfoWithSectionName:(sectionName)section
{
    NSDictionary* dic = self.sectionChannelList[section];
    
    return dic;
}

//根据不同的组设置不同的重用标识
-(NSString*)cellIdentifierWithSection:(NSInteger)section
{
    /*
     *static NSString* freeAudioCellID = @"HomeFreeAudioCellID";
     static NSString* specialColumnCellID = @"HomeSpecialColumnCellID";
     static NSString* everyDayBookCellID = @"HomeEveryDayBookCellID";
     static NSString* guessLikeCellID = @"HomeGuessLikeCellID";
     static NSString* rankDataCellID = @"HomeRankDataCellID";
     static NSString* recommendCellID = @"HomeRecommendCellID";
     static NSString* goldStenceCellID = @"HomeGoldStenceCellID";
     **/
    NSString* cellID = nil;
    
    switch (section)
    {
        case HMHomeFreeAudioSection://天天涨姿势，只有一行
            cellID = @"HomeFreeAudioCellID";
            break;
        case HMHomeSpecialColumnSection://行业达人,仅显示三行
            cellID = @"HomeSpecialColumnCellID";
            break;
        case HMHomeEveryDayBookSection://今今乐道
            cellID = @"HomeEveryDayBookCellID";
            break;
        case HMHomeGuessLikeSection://猜你喜欢,一个item上有三张图片
            cellID = @"HomeGuessLikeCellID";
            break;
        case HMHomeRankDataSection://热门排行数据
            cellID = @"HomeRankDataCellID";
            break;
        case HMHomeRecommendSection://推荐;
            cellID = @"HomeRecommendCellID";
            break;
        case HMHomeGoldStenceSection:
            cellID = @"HomeGoldStenceCellID";
            break;
        default:
            break;
    }
    return cellID;
}

-(NSInteger)numberOfItemsHomeTabWithSection:(NSInteger)section DataList:(NSArray*)homeList
{
    NSInteger itemsNum = 0;
    
    switch (section)
    {
        case HMHomeFreeAudioSection://天天涨姿势，只有一行
            itemsNum = 1;
            break;
        case HMHomeSpecialColumnSection://行业达人,仅显示三行
            itemsNum = 3;
            break;
        case HMHomeEveryDayBookSection://今今乐道
            itemsNum = [homeList[2] count];
            break;
        case HMHomeGuessLikeSection://猜你喜欢,一个item上有三张图片
            itemsNum = 1;
            break;
        case HMHomeRankDataSection://热门排行数据
            itemsNum = ([homeList[4] count] + 1.5)/3;
            break;
        case HMHomeRecommendSection://推荐
            itemsNum = 1;
            break;
        case HMHomeGoldStenceSection:
            itemsNum = 1;
            break;
        default:
            break;
    }
    return itemsNum;
}


//请求数据
-(void)loadHomeDataFinished:(void(^)(NSError *error))finished
{
    /*
     *Parameter 提示：首页参数 @{@"token" : nil}
     **/

    [[HMNetWorkTool sharedTool]requestHomeJsonDataWithParameter:nil Finished:^(id responseObject)
     {
         if ([responseObject isKindOfClass:[NSDictionary class]])
         {
             //定义存储解析完毕后的可变数组
             NSMutableArray* tempList = [NSMutableArray array];
             //             NSString* msg = nil;
             BOOL isSuccess = responseObject[@"success"];
             if (isSuccess)
             {
                 //                 msg = @"请求成功";
             }
             else
             {
                 //                 msg = @"请求失败";
             }
             //网络获取到的字典数据
             NSDictionary* dic = responseObject[@"data"];
             //获取轮播图数据的数组
             NSArray* carouselDicList = dic[@"carousel"];
             self.homeCarousel = [NSArray yy_modelArrayWithClass:[HTDCarousel class] json:carouselDicList];
             //第一组获取到天天涨姿势的数据数组
             NSArray* freeAudio = dic[@"free_audio"];
             //转换成模型数据
             NSArray* freeAudioList = [NSArray yy_modelArrayWithClass:[HMListenAnyTimeResModel class] json:freeAudio];
             [tempList insertObject: freeAudioList atIndex:0];
             
             //第二组行业达人数据
             NSArray * specialColumns = dic[@"special_column"];
             
             [tempList insertObject:[NSArray yy_modelArrayWithClass:[HTDSpecialColumn class] json:specialColumns] atIndex:1];
             
             //第三组今今乐道数据
             NSArray* everydayBooks = dic[@"everyday_book"];
             
             [tempList insertObject:[NSArray yy_modelArrayWithClass:[HTDEveryDayBook class] json:everydayBooks] atIndex:2];
             
             //第四组获取到猜你喜欢的数组
             NSArray* guessLikeList = dic[@"guess_like"];
             [tempList insertObject:[NSArray yy_modelArrayWithClass:[HTDGuessLike class] json:guessLikeList] atIndex:3];
             //第五组热门排行榜数据
             NSArray * rankingData = dic[@"ranking_data"][@"data"];
             [tempList insertObject:[NSArray yy_modelArrayWithClass:[HTDGuessLike class] json: rankingData] atIndex:4];
             //第六组第二行推荐的数据数组；隐藏组头组尾
             NSArray * recommendList = dic[@"recommend"];
             
             NSMutableArray* imgUrls = [NSMutableArray array];
             
             for (NSDictionary* dic in recommendList)
             {
                 [imgUrls addObject:dic[@"image_url"]];
             }
             [tempList insertObject:imgUrls.copy atIndex:5];
             //第七组看金句
             NSDictionary * goldStence = dic[@"gold_sentence"];
             [tempList insertObject:goldStence atIndex:6];
             self.homeData = tempList.copy;
             finished(nil);
         }
     } Failed:^(NSError *error)
     {
         NSLog(@"%@",error);
         finished(error);
     }];
}

-(NSArray *)sectionChannelList
{

    if (!_sectionChannelList)
    {
        _sectionChannelList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HMHomeTabSectionHeaderContent" ofType:@"plist"]];
    }
    return _sectionChannelList;
}
#pragma mark --懒加载

-(NSArray<HTDCarousel *> *)homeCarousel
{
    if (!_homeCarousel)
    {
        _homeCarousel = [NSArray array];
    }
    return _homeCarousel;
}

-(NSArray *)homeData
{
    if (!_homeData)
    {
        _homeData = [NSArray array];
    }
    return _homeData;
}

-(NSArray *)classifyList
{
    if (!_classifyList)
    {
        _classifyList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HMHomeClassification.plist" ofType:nil]];
    }
    return _classifyList;
}
@end
